function [ tstruct, Tt, Tnt ] = t_tide_totals( T, min_data, nodal, varargin )
% T_TIDE_TOTALS  t_tide tidal analysis for totals data in TUV structures
%
% This functions uses the T_tide toolbox of R. Pawlowicz et al.
% (http://www2.ocgy.ubc.ca/~rich/#T_Tide) to do tidal analysis on total
% currents data in a TUV structure.  It performs the analysis on the complex
% data U+i*V.  For more information about the results, see the t_tide
% function.
% 
% NOTE: t_tide must be on the path for this function to work.
%
% Usage: [TIDESTRUCT,Ttide,Tnotide] = t_tide_totals(TUV,min_data,nodal,prop,val,...)
%
% Inputs
% ------
% TUV = TUV structure with data to do tidal analysis on.
% min_data = minimum good data fraction at a grid point to do analysis.
%            See t_tide_matrix for more details and default value.
% nodal = boolean indicating whether or not to include nodal corrections
%         based on the latitude and timestamp contained in the TUV
%         structure. Defaults to true.  NOTE: If nodal corrections are
%         not done, then the phases in the TIDESTRUC will be relative to
%         the central time - this is very important for predicting
%         currents from the TIDESTRUC.
% prop,val = standard property,value pairs for the t_tide_matrix function.
%            These may override any such prop,value pairs that are
%            typically generated by the use of this function (such as
%            'latitude', 'start time' and 'output').
%
% Outputs
% -------
% TIDESTRUC = an array of tide structures with tidal constituents as
%             generated by t_tide.
% Ttide = TUV structure with just tidal predictions
% Tnotide = TUV structure with tide removed from original currents.
%
%
% NOTE: Times in input TUV structure should be in GMT for things to work
% best.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 	$Id: t_tide_totals.m 396 2007-04-02 16:56:29Z mcook $	
%
% Copyright (C) 2007 David M. Kaplan
% Licence: GPL
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if exist( 't_tide' ) < 2
  error( [ mfilename ' - Generic t_tide function must be on path.' ] );
end

if ~exist( 'nodal', 'var' ), nodal = true; end
if ~exist( 'min_data', 'var' ), min_data = []; end

varg = {};

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initial arguments to t_tide
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dt = diff( T.TimeStamp );
if range(dt) > 1/24/60/60
  warning( 'Time step size varies by more than 1 second.  WATCH OUT!' );
end
dt = dt(1);

% Add interval to beginning.
varg = [ varg, { 'output', 'none', 'interval', dt * 24 } ];

if nodal
  % Add start time and latitude
  % Latitude must be a cell array for t_tide_matrix
  lt = T.LonLat(:,2)';
  lt = num2cell( lt );
  
  varg = [ varg, { 'start time', T.TimeStamp(1), ...
                   'latitude', lt } ];
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Data to be tided
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
U = (T.U + i * T.V)'; % Transpose so rows are time

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tide analysis
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[tstruct,xx] = t_tide_matrix( U, min_data, varg{:}, varargin{:} );

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Output arguments
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargout > 1
  T.ProcessingSteps{end+1} = mfilename;
  T.OtherMetadata.(mfilename).min_data = min_data;
  T.OtherMetadata.(mfilename).nodal = nodal;
  T.OtherMetadata.(mfilename).varargin = varargin;
  
  Tt = T;
  Tt.U = real( xx )';
  Tt.V = imag( xx )';
  
  Tt.OtherMetadata.(mfilename).Type = 'tide';
end

if nargout > 2
  Tnt = T;
  Tnt.U = T.U - Tt.U;
  Tnt.V = T.V - Tt.V;

  Tt.OtherMetadata.(mfilename).Type = 'notide';
end
